<template>
  <yt-page title="风险详情" scroll>
    <yt-card title="基本信息">
      <yt-cell label="工艺/单元" :value="item.name"></yt-cell>
      <yt-cell label="场所/环节/部位" labelWidth="200px" :value="item.address"></yt-cell>
      <yt-cell label="危险和有害因素类别" labelWidth="200px" :value="item.genre"></yt-cell>
      <yt-cell label="事故类型" :value="item.type"></yt-cell>
      <yt-cell label="登记时间" :value="item.time"></yt-cell>
      <yt-cell label="危险源辨识内容" labelWidth="30%" :value="item.des"></yt-cell>
      <yt-cell label="所属网格" labelWidth="30%" :value="item.gridId.attributes.name" v-if="item.gridId"></yt-cell>
      <yt-cell v-if="item.gridId" label="周边设备" isLink @click="goDevicePage"></yt-cell>
      <yt-cell v-if="item.gridId" label="周边视频" isLink @click="goVideoPage"></yt-cell>
    </yt-card>
    <yt-card title="风险评估">
      <yt-cell label="评估标准" :value="item.criteria"></yt-cell>
      <yt-cell v-for="(obj, index) in item.contents" labelWidth="200px" :label="getLabel(index)" :value="obj.name" :key="index"></yt-cell>
    </yt-card>
    <yt-card title="评估结果">
      <yt-cell label="风险值" :value="getScore"></yt-cell>
      <yt-cell label="风险等级" :value="item.level"></yt-cell>
      <yt-cell label="风险描述" :value="getDesc"></yt-cell>
      <yt-cell label="可视级别">
        <span v-if="item.level === '1级'" class="yt-rate-item"><i class="yt-icon yt-rate-icon icon-star-on" style="color: red"></i></span>
        <span v-if="item.level === '2级'" class="yt-rate-item"><i class="yt-icon yt-rate-icon icon-star-on" style="color: orange"></i></span>
        <span v-if="item.level === '3级'" class="yt-rate-item"><i class="yt-icon yt-rate-icon icon-star-on" style="color: yellow"></i></span>
        <span v-if="item.level === '4级'" class="yt-rate-item"><i class="yt-icon yt-rate-icon icon-star-on" style="color: blue"></i></span>
      </yt-cell>
    </yt-card>
    <yt-card title="管控信息">
      <yt-cell label="负责部门" :value="item.department"></yt-cell>
      <yt-cell label="负责人" :value="item.responsible"></yt-cell>
      <yt-cell label="管控级别" :value="item.grade"></yt-cell>
      <yt-textarea disabled title="管控措施" v-model="item.control" border="half"></yt-textarea>
    </yt-card>
  </yt-page>
</template>

<script>
import { commonApi } from '@/common/api/commonApi.js'
import { ReplaceDictionary } from '@/common/utils/index.js'
export default {
  data () {
    return {
      item: {},
      criteriaObj: null,
      xing: 5,
      xingColors: ['red', 'yellow', 'blue', 'green']
    }
  },
  computed: {
    getScore () {
      const _default = 0
      if (!this.item.contents || !this.criteriaObj) return _default
      let contents = Object.values(this.item.contents)
      if (contents.length === 0) return _default
      let rules = this.criteriaObj.rules
      let results = contents.forEach((v, index) => {
        let _contdent = this.criteriaObj.contents[index]
        rules = rules.replace(_contdent.alias, v.score)
      })
      let score = 0
      try {
        score = eval(rules)
      } catch (e) {
      }
      return score
    },
    getDesc () {
      if (!this.criteriaObj) return ''
      let obj = this.criteriaObj.grades.find(v => v.name === this.item.level)
      return obj ? obj.desc : ''
    }
  },
  activated () {
    this.getData()
  },
  methods: {
    getLabel(index) {
      if (!this.criteriaObj) return ''
      let item = this.criteriaObj.contents[index]
      return item ? item.name : ''
    },
    async getData () {
      let items = await commonApi.search('security_risk_corporate', { equalTo: { objectId: this.$route.query.objectId }, include: ['gridId'] })
      let item = items[0]
      let p = [
        commonApi.searchById('SecurityRiskControlCriteria', item.criteria),
        commonApi.dictionary('disaster'),
        commonApi.dictionary('risk_type'),
        commonApi.dictionary('Hierarchy')
      ]
      let [ criteriaObj, disasterDic, riskTypeDic, Hierarchy ] = await Promise.all(p)
      let list = [
        {
          D_text: criteriaObj.name,
          D_code: criteriaObj.id
        }
      ]
      this.criteriaObj = criteriaObj
      this.item = ReplaceDictionary(item, { type: disasterDic, genre: riskTypeDic, criteria: list, grade: Hierarchy }) 
    },
    goDevicePage () {
      this.$router.$push({ path: '/security-risk/detail/device', query: {gridId: this.$route.query.gridId} })
    },
    goVideoPage () {
      this.$router.$push({ path: '/security-risk/detail/video', query: {gridId: this.$route.query.gridId} })
    }
  }
}
</script>

<style lang="stylus" scoped>
>>>.yt-card-body 
  padding 0
</style>
